var coordinateID = 'coordinate';
var oldapplydata;
var applyid;

$(function(){

	bindDict('worktypecode', 'DM_SSSXZYLX', null, null);
//	bindDict('workpositioncode', 'DM_SSSXZYWZ', null, null);
	bindDict('workmethodcode', 'DM_SSSXZYFS', null, null);
	bindDict('changetypecode', 'DM_BGLX', null, null);
	$('.changeTime').hide();
	$('.changeShip').hide();
	$('.change').hide();
	
	$('#changetypecode').change(function(){
		var changetypecode = $(this).val();
		if(changetypecode == '01'){
			$('.changeTime').show();
			$('.changeShip').hide();
		}
		else if(changetypecode == '02'){
			$('.changeTime').hide();
			$('.changeShip').show();
		}
		else{
			$('.changeTime').hide();
			$('.changeShip').hide();
		}
	});
	
	$('#oldapplyno').change(function(){
		var oldapplyno = $(this).val();
		bindApply1(oldapplyno);
	})
	
	$('#seeInfo').click(function(){
		var oldapplyno = $('#oldapplyno').val();
		window.open("/gahCSP/business/detail.html?applyno="+oldapplyno+"&itemno=1002");
	})
})


function bindApply1(applyno){
	$.ajax({ 
		url: basePath() + 'business/getapplyinfo.do',
		data: {"applyno": applyno},
		dataType: "json",
        contentType: "application/json",
		success: function(data){
	    	if(data.result == 1){
	    		var jsonApply = data.datas.apply;
	    		var jsonBusiness = data.datas.business;
	    		var jsonMaterial = data.datas.material;
	    		var jsonShip = data.datas.ship;
	    		var jsonCoordinate = data.datas.coordinate;
	    		
	    		applyid = jsonApply.applyid;
	    		
	    		bindApplyForm1(jsonApply);
	    		
	    		
	    		if(jsonApply['nodename'] != '办结' || jsonApply['itemno'] != '1002'){
					$.alert({
					    title: '提示!',
					    content: '<span style="color:red;">办结</span>后的【通航水域水上水下活动作业许可】才能进行变更',
					    confirmButton: '确定'
					});
		    		return;	
	    		}
	    		
	    		
	    		//绑定业务资料
	    		bindBusinessForm1(jsonBusiness);
	    		
	    		//绑定附件材料
	    		bindMaterialListForm(jsonMaterial);	  
	    		
	    		//绑定船舶数据
	    		if($('#ship').length > 0){
		    		bindShipForm(jsonShip[0]);	 
	    		}
	    		else{ 
		    		if(jsonShip != '' && undefined != jsonShip){
		    			for(var i=0; i<jsonShip.length; i++){
		    				var data = jsonShip[i];
		    				bindShipListBefore(data);
		    				bindShipList(data);
		    				$("#empty").remove();
//		    				check(data);
		    			}
		    		}
	    		}

	    		//绑定坐标
	    		if(jsonCoordinate != ''){
	    			for(var i=0; i<jsonCoordinate.length; i++){
	    				var data = jsonCoordinate[i];
	    				bindCoordinateList(data);
	    			}
	    		}

	    		//查询文书
//	    		getBookByItemnoAndNodecode(data.datas.nodeno);
	    	}
      	}
	});
}


function bindApplyForm1(jsonApply){
	orgcode = jsonApply.orgcode;
	
	$('#orgcode').val(jsonApply.orgcode);
	$('#orgname').val(jsonApply.orgname);
	$('#org').val(jsonApply.orgname);
	$('#org').attr('data', jsonApply.orgcode);
	$('#applyid').val(jsonApply.applyid);
	$('#applyno').val(jsonApply.applyno);
//	$('.applyno').html(jsonApply.applyno);
//	$('.applyno').val(jsonApply.applyno);
//	$('.applyid').val(jsonApply.applyid);
	

//	$('[name="itemno"]').val(jsonApply.itemno);
//	$('[name="itemname"]').val(jsonApply.itemname);
//	$('[name="itemtypecode"]').val(jsonApply.itemtypecode);
//	$('#title').html(jsonApply.itemname);
//	document.title = jsonApply.itemname;
}

function bindBusinessForm1(jsonBusiness){
	var waterwaycode = '';
	var markcode = '';
	
	for(var d in jsonBusiness){//遍历json对象的每个key/value对,p为key 
		if (d == 'workbegintime' || d == 'workendtime'
				|| d == 'executionbegintime' || d == 'executionendtime'
				|| d == 'workstarttime' || d == 'delaydate'
				|| d == 'ensurebegintime' || d == 'ensureendtime') {
			$('[name=' + d + ']').val(formatDate(jsonBusiness[d], 'yyyy-mm-dd'));
		}
		else if(d == 'waterwaycode'){
			bindWaterway('waterway', $('#orgcode').val(), jsonBusiness[d]);		
			waterwaycode = jsonBusiness[d];
		}
		else if(d == 'berthcode'){
		    bindBearth('berthcode', orgcode , jsonBusiness[d]);
		}
		else if(d == 'worktype'){
			var worktypes = jsonBusiness[d].split(',');

		    bindDict1('worktype', 'DM_BBZYZL', worktypes);			
		}
		else if (d == 'changetypecode'){
			bindDict1('changetypecode', 'DM_BGLX', jsonBusiness[d]);
		}
		else if(d == 'navigationmarkcode'){
			markcode = jsonBusiness[d];
		}
		else if(jsonBusiness['oldapplyno'] == '' && (d == 'oldapplyno' || d == 'changetypecode' || d == 'changecontent' )){
			
		}
		else{
			$('[name=' + d + ']').val(jsonBusiness[d]);
		}
	}
	
	if(jsonBusiness['oldapplyno'] != ''){
		$('#oldapplyno').val(jsonBusiness['oldapplyno']);
		
		var changetypecode = $('#changetypecode').val();
		if(changetypecode == '01'){
			$('.changeTime').show();
			$('.changeShip').hide();
		}
		else if(changetypecode == '02'){
			$('.changeTime').hide();
			$('.changeShip').show();
		}
		else{
			$('.changeTime').hide();
			$('.changeShip').hide();
		}
	}
	if(applyid != ''){
		$('#oldapplyid').val(applyid);
	}
}


function bindShipForm(jsonShip){
	if(undefined != jsonShip){
		$('#ship').val(jsonShip.shipname);
		$('#ship').attr('data', JSON.stringify(jsonShip));
	}
}


function bindShipListBefore(data){
//	var str;
//	if(data.verifyresult == 1)
//		str = '<img src="../img/jiaoyan1.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == 0)
//		str = '<img src="../img/jiaoyan2.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == -1)
//		str = '<img src="../img/jiaoyan3.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 

	var htmlStr = ''+		
				'<tr>'+
				'	<td>'+ data.shipidnumber +'</td>'+
				'	<td>'+ data.shipname +'</td>'+
//				'	<td>'+ data.technicalparameter +'</td>'+
				'</tr>';

	$('#TableShipBefore').append(htmlStr);
}
function bindShipList(data){
//	var str;
//	if(data.verifyresult == 1)
//		str = '<img src="../img/jiaoyan1.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="checkInfo('+JSON.stringify(data.verify).replace(/\"/g,"'")+')" title="点击查看详情">'; 
//	else if(data.verifyresult == 0)
//		str = '<img src="../img/jiaoyan2.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="checkInfo('+JSON.stringify(data.verify).replace(/\"/g,"'")+')" title="点击查看详情">'; 
//	else if(data.verifyresult == -1)
//		str = '<img src="../img/jiaoyan3.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="checkInfo('+JSON.stringify(data.verify).replace(/\"/g,"'")+')" title="点击查看详情">'; 
//	else   //正在校验中
//		str = '<img src="../img/loading.gif" alt="" style="width:20px;height:20px;" onclick="" title="数据校验中">';
	var technicalparameter = '';
	if('technicalparameter' in data){
		technicalparameter = data.technicalparameter;
	}
	var htmlStr = ''+		
				'<tr id="tr_'+ data.shipidnumber +'">'+
				'	<td>'+ data.shipidnumber +'</td>'+
				'	<td>'+ data.shipname +'</td>'+
//				'	<td><input type="text" class="form-control" id="'+ data.shipidnumber +'" value="'+ technicalparameter +'" /></td>'+
				'	<td>'+
				'		<input type="hidden" name="ship" id="ship'+data.shipidnumber+'" value=\''+ JSON.stringify(data) +'\'>'+
				'		<button onclick="deleteShip(\''+data.shipidnumber+'\');" type="button" class="btn btn-danger btn-sm" style="margin-right: 10px;">'+
				'			<span class="glyphicon glyphicon-minus"></span>'+
				'		</button>'+
				'	</td>'+
				'</tr>';

	$('#TableShip').append(htmlStr);
}

function deleteShip(shipidnumber){
	$('#tr_' + shipidnumber).remove();
}


function bindShipInfoList(data){
//	var str = "";
//	if(data.verifyresult >0)
//		str = '<img src="../img/jiaoyan1.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == 0)
//		str = '<img src="../img/jiaoyan2.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == -1)
//		str = '<img src="../img/jiaoyan3.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
	
	var htmlStr = ''+		
				'<tr>'+
				'	<td>'+ data.shipidnumber +'</td>'+
				'	<td>'+ data.shipname +'</td>'+
//				'	<td>'+ data.technicalparameter +'</td>'+
				'</tr>';
	
	$('#TableShip').append(htmlStr);
}


function bindShipEditList(data){
//	var str;
//	if(data.verifyresult >0)
//		str = '<img src="../img/jiaoyan1.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == 0)
//		str = '<img src="../img/jiaoyan2.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == -1)
//		str = '<img src="../img/jiaoyan3.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
	var htmlStr = ''+		
	'<tr id="tr_'+ data.shipidnumber +'">'+
	'	<td>'+ data.shipidnumber +'</td>'+
	'	<td>'+ data.shipname +'</td>'+
//	'	<td><input type="text" class="form-control" name="technicalparameter" id="'+data.shipidnumber+'" value=\"'+ data.technicalparameter +'"/></td>'+
	'	<td>'+
	'		<input type="hidden" name="ship" id="ship'+data.shipidnumber+'" value=\''+ JSON.stringify(data) +'\'>'+
	'		<button onclick="deleteShip(\''+data.shipidnumber+'\');" type="button" class="btn btn-danger btn-sm" style="margin-right: 10px;">'+
	'			<span class="glyphicon glyphicon-minus"></span>'+
	'		</button>'+
	'	</td>'+
	'</tr>';

	$('#TableShip').append(htmlStr);
}

var index = 1;
function bindCoordinateList(data){
    
	
	var htmlStr = ''+		
				'  <tbody class="coordinateForm">'+
				'  <tr class="tr_'+ index +'">'+
				'    <td rowspan="2" style="vertical-align: middle;">'+
				'    	<input type="number" class="form-control serialnumber" style="text-align: center;" value="'+ index +'" name="serialnumber" no="N" />'+
				'	</td>'+
				'    <td>   '+
				'	      <div class="form-group">'+
				'		    <label class="col-sm-4 control-label">经度</label>'+
				'		    <div class="col-sm-8">'+
				'			<select class="form-control" name="lonarea" no="N">'+
				'				<option value="E">E</option>'+
				'				<option value="W">W</option>'+
				'			</select>'+
				'		    </div>'+
				'		  </div> '+
				'    </td>'+
				'    <td><input type="number" class="form-control" placeholder="度" value="0" name="londegree" no="N"></td>'+
				'    <td><input type="number" class="form-control" placeholder="分" value="0" name="lonmin" no="N"></td>'+
				'    <td><input type="number" class="form-control" placeholder="秒" value="0" name="lonsecond" no="N"></td>'+
				'    <td rowspan="2" style="vertical-align: middle;">'+
				'		<button type="button" onclick="coordinateDelete('+ index +');" class="btn btn-danger btn-sm" style="margin-right: 10px;">'+
				'			<span class="glyphicon glyphicon-minus"></span>'+
				'		</button>'+
				'	  </td>'+
				'  </tr>'+
				'  <tr class="tr_'+ index +'">'+
				'    <td>'+
				'	      <div class="form-group">'+
				'		    <label class="col-sm-4 control-label">纬度</label>'+
				'		    <div class="col-sm-8">'+
				'				<select class="form-control" name="latarea" no="N">'+
				'					<option value="N">N</option>'+
				'					<option value="S">S</option>'+
				'				</select>'+
				'		    </div>'+
				'		  </div> '+
				'	  </td>'+
				'    <td><input type="number" class="form-control" placeholder="度" value="0" name="latdegree" no="N"></td>'+
				'    <td><input type="number" class="form-control" placeholder="分" value="0" name="latmin" no="N"></td>'+
				'    <td><input type="number" class="form-control" placeholder="秒" value="0" name="latsecond" no="N"></td>'+
				'  </tr>'+
				'  </tbody>';
	
	$('#' + coordinateID).append(htmlStr);
	
	if(data != null){
		var serialnumber = data.serialnumber;
		var latarea = data.latarea;
		var latdegree = data.latdegree;
		var latmin = data.latmin;
		var latsecond = data.latsecond;
		var lonarea = data.lonarea;
		var londegree = data.londegree;
		var lonmin = data.lonmin;
		var lonsecond = data.lonsecond;
		
		$('.tr_' + index).find('[name="serialnumber"]').val(serialnumber);
		$('.tr_' + index).find('[name="latarea"]').val(latarea);
		$('.tr_' + index).find('[name="latdegree"]').val(latdegree);
		$('.tr_' + index).find('[name="latmin"]').val(latmin);
		$('.tr_' + index).find('[name="latsecond"]').val(latsecond);
		$('.tr_' + index).find('[name="lonarea"]').val(lonarea);
		$('.tr_' + index).find('[name="londegree"]').val(londegree);
		$('.tr_' + index).find('[name="lonmin"]').val(lonmin);
		$('.tr_' + index).find('[name="lonsecond"]').val(lonsecond);
	}

	index++;
}


function bindCoordinateInfoList(data){
	var serialnumber = data.serialnumber;
	var latarea = data.latarea;
	var latdegree = data.latdegree;
	var latmin = data.latmin;
	var latsecond = data.latsecond;
	var lonarea = data.lonarea;
	var londegree = data.londegree;
	var lonmin = data.lonmin;
	var lonsecond = data.lonsecond;
	
	var htmlStr = ''+		
				'  <tbody class="coordinateForm">'+
				'  <tr>'+
				'    <td rowspan="2" style="vertical-align: middle;">'+
				'		'+ serialnumber +''+
				'	 </td>'+
				'    <td>   '+
				'	      <div class="form-group">'+
				'		    <label class="col-sm-4 control-label">经度</label>'+
				'		    <div class="col-sm-8">'+
				'				'+ latarea +''+
				'		    </div>'+
				'		  </div> '+
				'    </td>'+
				'    <td>'+ latdegree +'</td>'+
				'    <td>'+ latmin +'</td>'+
				'    <td>'+ latsecond +'</td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td>'+
				'	      <div class="form-group">'+
				'		    <label class="col-sm-4 control-label">纬度</label>'+
				'		    <div class="col-sm-8">'+
				'				'+ lonarea +''+
				'		    </div>'+
				'		  </div> '+
				'	  </td>'+
				'    <td>'+ londegree +'</td>'+
				'    <td>'+ lonmin +'</td>'+
				'    <td>'+ lonsecond +'</td>'+
				'  </tr>'+
				'  </tbody>';
	
	$('#' + coordinateID).append(htmlStr);
}

function coordinateDelete(index){
	$('.tr_' + index).remove();
}
